﻿using Abp.Application.Services.Dto;
using shenghua.Basics.ShippingTemplates;
using shenghua.Basics.ShippingTemplateSetting.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Basics.ShippingTemplateSetting
{
    public class ShippingTemplateAppService: shenghuaAppServiceBase, IShippingTemplateAppService
    {
        private readonly ShippingTemplateManager _shippingTemplateManager;

        public ShippingTemplateAppService(
            ShippingTemplateManager shippingTemplateManager
            )
        {
            _shippingTemplateManager = shippingTemplateManager;
        }

        public async Task<ShippingTemplateDto> Create(CreateShippingTemplateDto input)
        {
            ShippingTemplate shippingTemplate = new ShippingTemplate
            {
                Name = input.Name,
                DisplayName = input.DisplayName,
                Freight = input.Freight,
                FirstNumber = input.FirstNumber,
                AddedNumber = input.AddedNumber,
                AddedFreight = input.AddedFreight,
                TemplateType = input.TemplateType
            };
            await _shippingTemplateManager.Add(shippingTemplate);
            return new ShippingTemplateDto(shippingTemplate);
        }

        public async Task<ShippingTemplateDto> Update(UpdateShippingTemplateDto input)
        {
            ShippingTemplate shippingTemplate = await _shippingTemplateManager.Get(input.Id);
            shippingTemplate.DisplayName = input.DisplayName;
            shippingTemplate.Freight = input.Freight;
            shippingTemplate.FirstNumber = input.FirstNumber;
            shippingTemplate.AddedNumber = input.AddedNumber;
            shippingTemplate.AddedFreight = input.AddedFreight;
            shippingTemplate.TemplateType = input.TemplateType;
            await _shippingTemplateManager.Update(shippingTemplate);
            return new ShippingTemplateDto(shippingTemplate);
        }

        public async Task Delete(EntityDto input)
        {
            await _shippingTemplateManager.Delete(input.Id);
        }

        public async Task<List<ShippingTemplateDto>> GetAll()
        {
            var list = await _shippingTemplateManager.GetAll();
            return list.Select(r => new ShippingTemplateDto(r)).ToList();
        }

        public async Task SetEnabled(EntityDto input)
        {
            await _shippingTemplateManager.Enabled(input.Id, true);
        }

        public async Task SetDisabled(EntityDto input)
        {
            await _shippingTemplateManager.Enabled(input.Id, false);
        }

        public async Task SetTemplateType(UpdateShippingTemplateTypeDto input)
        {
            await _shippingTemplateManager.SetTemplateType(input.Id, input.TemplateType);
        }

        public async Task SetAreas(UpdateShippingTemplateAreasDto input)
        {
            await _shippingTemplateManager.SetAreas(input.Id, input.Areas);
        }

        public async Task SetFreeShipping(UpdateShippingTemplateFreeShippingDto input)
        {
            await _shippingTemplateManager.SetFreeShipping(input.Id, input.AllowFreeShipping);
        }

        public async Task SetFreeShippingConditions(UpdateShippingTemplateFreeShippingConditionsDto input)
        {
            var conditions = ObjectMapper.Map<List<FreeShippingCondition>>(input.FreeShippingConditions);
            await _shippingTemplateManager.SetFreeShippingConditions(input.Id, conditions);
        }
    }
}
